package com.aaa.service.impl;

import com.aaa.dao.MenuDao;
import com.aaa.dao.UserDao;
import com.aaa.entity.Dept;
import com.aaa.entity.Menu;
import com.aaa.entity.Role;
import com.aaa.entity.User;
import com.aaa.service.MenuService;
import com.aaa.utils.JwtUtil;
import com.aaa.utils.WebUtil;
import com.aaa.vo.MenuVo;
import com.aaa.vo.Result;
import com.aaa.vo.RoleVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuDao, Menu> implements MenuService {
    @Autowired
    private MenuDao menuDao;
    @Override
    public List<Menu> findByUserid(Long userid) {
        return menuDao.selectByUserid(userid);
    }


    @Override
    public Result<List<Menu>> getLeftMenu() {
        //获取当前用户的手机号
        String token = WebUtil.getRequest().getHeader("token");
        Map<String, Object> map = JwtUtil.getTokenChaim(token);
        String phone = (String) map.get("username");
        //该用户具有的菜单
        List<Menu> menus = menuDao.findLeftMenu(phone);

        //1.查询所有的一级菜单
        List<Menu> firsts=menus.stream().filter(item->item.getParentId()==0).collect(Collectors.toList());

        //2.查询一级菜单对应的二级菜单---只适合只有两级菜单
        for(Menu f:firsts){
            f.setChildren(getChildren(f.getMenuId(),menus));
        }

        return new Result<List<Menu>>(2000,"查询菜单成功",firsts);
    }

    @Override
    public Result<List<Menu>> getAll() {
        QueryWrapper<Menu> wrapper=new QueryWrapper<>();
        wrapper.eq("status",0);
        List<Menu> menus = menuDao.selectList(wrapper);
        //1.查询所有的一级菜单
        List<Menu> firsts=menus.stream().filter(item->item.getParentId()==0).collect(Collectors.toList());
        //2.查询一级菜单对应的二级菜单---只适合只有两级菜单
        for(Menu f:firsts){
            f.setChildren(getChildren(f.getMenuId(),menus));
        }

        return new Result<List<Menu>>(2000,"查询菜单成功",firsts);
    }

    @Override
    public Result<List<Integer>> findByRoleId(Long roleId) {
        List<Integer> menuIds= menuDao.getByRoleId(roleId);
        return new Result<>(2000,"查询选中的菜单id",menuIds);
    }

    @Transactional
    @Override
    public Result<String> confirmMenu(Long roleId, Long[] menuIds) {
        //roleId删除具有的菜单id
        menuDao.deleteByRoleId(roleId);
        //批量添加
        menuDao.batchMenus(roleId,menuIds);
        return new Result<>(2000,"分配成功");
    }
        //分页查询所以菜单
    @Override
    public Result<IPage<Menu>> MenuPage(Integer currentPage, Integer pageSize, MenuVo menuVo) {
        IPage<Menu> page=new Page<>(currentPage,pageSize);
        QueryWrapper<Menu> wrapper=new QueryWrapper<>();
        if(StringUtils.hasText(menuVo.getMenuName())){
            wrapper.like("menu_name",menuVo.getMenuName());
        }
        if(StringUtils.hasText(menuVo.getMenuType())){
            wrapper.eq("menu_type",menuVo.getMenuType());
        }
        if(StringUtils.hasText(menuVo.getPath())){
            wrapper.eq("path",menuVo.getPath());
        }
        if(Objects.nonNull(menuVo.getPercode())){
            wrapper.eq("percode",menuVo.getPercode());
        }
        if(Objects.nonNull(menuVo.getStatus())){
            wrapper.eq("status",menuVo.getStatus());
        }
        if(Objects.nonNull(menuVo.getDateRange())&&menuVo.getDateRange().length==2){
            wrapper.between("create_time",menuVo.getDateRange()[0],menuVo.getDateRange()[1]);
        }
        IPage<Menu> page1 = menuDao.selectPage(page, wrapper);
        return new Result<IPage<Menu>>(2000,"查询角色",page1);
    }
    //添加修改
    @Override
    public boolean saveAndUpdate(Menu menu) {
        int i=-1;
        if (menu.getCreateTime()==null){
            menu.setCreateTime(new Date());
            menu.setUpdateTime(new Date());
        }else {
            menu.setUpdateTime(new Date());
        }
        if(menu.getMenuId()==null){
            i=menuDao.insert(menu);
            System.out.println("====="+menu.getStatus());
        }else {
            i=menuDao.updateById(menu);
        }
        if (i>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean delById(Long id) {
        int i = menuDao.deleteById(id);
        if (i > 0) {
            return true;
        }
        return false;
    }




    private List<Menu> getChildren(Long parentId,List<Menu> menus){
        List<Menu> seconds=new ArrayList<>();
        for(Menu m:menus){
            if(m.getParentId().equals(parentId)){
                seconds.add(m);
            }
        }
        for(Menu m:seconds){
            m.setChildren(getChildren(m.getMenuId(),menus));
        }

        return seconds;

    }



}
